package com.zwh.tool.utils;


import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.OutputStream;

public class SevenZUtil {

//    public static void main(String[] args) {
//        try {
//            // 要压缩的文件或文件夹
//            Path sourcePath = Paths.get("/path/to/source");
//
//            // 压缩后的7z文件
//            Path outputPath = Paths.get("/path/to/output.7z");
//
//            // 创建7z格式的压缩包
//            create7zArchive(sourcePath, outputPath);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

//    private static void create7zArchive(Path sourcePath, Path outputPath) throws Exception {
//        IOutCreateArchive<IOutItemAllFormats> outCreateArchive = SevenZip.openOutArchive(SevenZip.FORMAT_7Z);
//
//        try {
//            // 添加要压缩的文件或文件夹
//            List<File> filesToAdd = getFilesToAdd(sourcePath.toFile());
//            for (File file : filesToAdd) {
//                IOutItem7z outItem = OutItemFactory.create7zOutItem(file);
//                outCreateArchive.addArchiveItem(outItem, false);
//            }
//
//            // 设置压缩参数，例如压缩级别等
//            IOutCreateArchive.IOutCreateWithSW setProps = outCreateArchive;
//            setProps.setLevel(5); // 设置压缩级别，1-9，数字越大，压缩率越高
//
//            // 创建并写入压缩包
//            try (IOutCreateArchive<IOutItemAllFormats> finalizedOutArchive = outCreateArchive.execute()) {
//                finalizedOutArchive.getArchiveOutputStream().setOutput(Files.newOutputStream(outputPath));
//            }
//
//        } finally {
//            outCreateArchive.close();
//        }
//    }
//
//    private static List<File> getFilesToAdd(File source) throws IOException {
//        List<File> filesToAdd = new ArrayList<>();
//        if (source.isDirectory()) {
//            Files.walk(source.toPath())
//                    .filter(path -> Files.isRegularFile(path))
//                    .map(Path::toFile)
//                    .forEach(filesToAdd::add);
//        } else {
//            filesToAdd.add(source);
//        }
//        return filesToAdd;
//    }

        public static void main(String[] args) throws Exception {
            String filePath = "D:/idea/file/file.xlsx";
            // 创建工作簿
            Workbook workbook = new XSSFWorkbook();
            // 创建工作表
            Sheet sheet = workbook.createSheet("Sheet1");
            // 创建单元格并添加数据
            Row row = sheet.createRow(0);
            Cell cell = row.createCell(0);
            cell.setCellValue("Hello, World!");
            // 加密Excel文件
            String password = "password";
            POIFSFileSystem fs = new POIFSFileSystem();
            EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
            Encryptor enc = info.getEncryptor();
            enc.confirmPassword(password);
            OutputStream os = enc.getDataStream(fs);
            workbook.write(os);
            os.close();
            // 导出加密的Excel文件
//            String filePath = "path/to/your/encrypted/file.xlsx";
            try (FileOutputStream outputStream = new FileOutputStream(filePath)) {
                fs.writeFilesystem(outputStream);
            }
            System.out.println("Encrypted Excel file exported successfully!");
        }


}
